package com.fla.millioner.ui;

import com.alone1672.millioner.R;
import com.danikula.aibolit.Aibolit;
import com.danikula.aibolit.annotation.InjectOnClickListener;
import com.danikula.aibolit.annotation.InjectService;
import com.danikula.aibolit.annotation.InjectView;
import com.fla.adprovider.AdManager;
import com.fla.millioner.MillionerApplication;
import com.fla.millioner.QuestionService;
import com.fla.millioner.Utils;
import com.fla.millioner.ui.support.AndroidUtils;
import com.fla.millioner.ui.support.ProgressedAsyncTask;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends Activity {
    private static final int GAME_CODE = 0;

    private static final String GAME_FINISHED_COUNT_KEY = "GAME_FINISHED_COUNT_KEY";

    @InjectService
    private MillionerApplication millionerApplication;

    @InjectService
    private QuestionService questionService;

    @InjectView(R.id.bestResultTextView)
    private TextView bestResultTextView;

    @InjectView(R.id.bestResultLabel)
    private TextView bestResultLabel;

    @InjectView(R.id.startGameButton)
    private TextView startGameButton;

    @InjectView(R.id.rateButton)
    private TextView rateButton;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Aibolit.setInjectedContentView(this, R.layout.main_activity);

        Utils.setVisibility(!MillionerApplication.FOR_SAMSUNG, rateButton);

        new DBLoader(this).execute();

        AdManager.getInstance().identifyAdTypes(this);
        AdManager.getInstance().getAdProvider().loadAndCacheInterstitialAd(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        bestResultTextView.setText(millionerApplication.getBestResult() + " $");
        applyTextSize();
    }

    private void applyTextSize() {
        int size = millionerApplication.getCurrentFontSize();

        bestResultLabel.setTextSize(size);
        bestResultTextView.setTextSize(size);
        startGameButton.setTextSize(size);
        rateButton.setTextSize(size);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);

        int count = preferences.getInt(GAME_FINISHED_COUNT_KEY, 0);
        boolean mustShowAd = count % 3 == 0;

        if (mustShowAd) {
            AdManager.getInstance().getAdProvider().showInterstitialAd(this);
            AdManager.getInstance().getAdProvider().loadAndCacheInterstitialAd(this);
            preferences.edit().putInt(GAME_FINISHED_COUNT_KEY, count + 1).commit();
        }

        if (!mustShowAd) {
            preferences.edit().putInt(GAME_FINISHED_COUNT_KEY, count + 1).commit();
        }
    }

    @InjectOnClickListener(R.id.startGameButton)
    private void onStartGameCLicked(View view) {
        startActivityForResult(new Intent(this, GameActivity.class), GAME_CODE);
    }

    @InjectOnClickListener(R.id.rateButton)
    private void onRateCLicked(View view) {
        AndroidUtils.launchGooglePlay(this);
    }

    private class DBLoader extends ProgressedAsyncTask<Void, Void, Void> {

        public DBLoader(Context context) {
            super(context);
        }

        @Override
        protected Void executeInBackground(Void... params) throws Exception {
            questionService.load();
            return null;
        }

    }
}
